package cn.hengzhu.main_manager.web.xcx.goods;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.hengzhu.main_manager.repository.dao.ManageCaseBoxMapper;
import cn.hengzhu.main_manager.repository.domain.ManageCaseBox;
import cn.hengzhu.main_manager.repository.domain.ManageCaseBoxExample;
import cn.hengzhu.main_manager.repository.vo.GoodsPriceVO;
import cn.hengzhu.main_manager.utils.AjaxResult;
import cn.hengzhu.main_manager.utils.ann.MoblieUrl;



/**
 * @author 周磊
 * @since 2018年10月24日
 */
@Controller
@RequestMapping("/goods")
public class GoodsController {
	
	@Autowired
	private ManageCaseBoxMapper manageCaseBoxMapper;
	/**
	 * @MoblieUrl 此注解作用此时相当于是一个标识 加上此注解会有一个切面处理
	 * @author 周磊
	 * @since 2018年10月24日
	 * @return
	 * @param price 
	 * @throws Exception 
	 */
	@MoblieUrl
	@RequestMapping("/priceChange")
	public String goods(String caseNo,Model model,String asd,HttpServletRequest request){
		ManageCaseBoxExample me = new ManageCaseBoxExample();
		me.createCriteria().andCaseNoEqualTo(caseNo).andIsDelEqualTo((byte) 0);
		List<ManageCaseBox> selectByExample = manageCaseBoxMapper.selectByExample(me);
		model.addAttribute("list", selectByExample);
		model.addAttribute("caseNo", caseNo);
		return request.getAttribute("moblie")+"goodsPrice";
	}
	
	/**
	 * @author 周磊
	 * @since 2018年10月25日
	 * @param prices
	 * @param ids
	 * @return
	 */
	@RequestMapping("/postPrice")
	@ResponseBody
	public AjaxResult postPrice(String prices,String ids,String caseNo){
		List<GoodsPriceVO> list = new ArrayList<>();
		
		List<String> priceList = Arrays.asList(prices.split(","));
		List<String> idList = Arrays.asList(ids.split(","));
		
		for (int i =0; i < idList.size(); i++) {
			GoodsPriceVO gv = new GoodsPriceVO();
			gv.setId(Integer.parseInt(idList.get(i)));
			gv.setPrice(priceList.get(i));
			list.add(gv);
		}
		Integer num = manageCaseBoxMapper.updatePrices(list);
		if(num == 0 ) {
			return new AjaxResult("修改失败");
		}
		return new AjaxResult();
	}
}




